/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: RemoteControl.java Package: tc.de.krawallmann.robobridge.visualization
 * Version: 1.0 Author(s): KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.visualization;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import tc.de.krawallmann.robobridge.RoboBridge;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.implementation.Direction;
import tc.de.krawallmann.robobridge.devices.implementation.RP6Base;


/**
 * The Class RemoteControl.<br/>
 * Partially auto-generated Javadoc!
 */
public class RemoteControl extends AbstractRoboViewDisplay {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5058048976295517777L;

	/** The base. */
	private final RP6Base base;

	/** The bridge. */
	private final RoboBridge bridge;

	/** The executing. */
	private boolean executing;

	/**
	 * Instantiates a new remote control.
	 * 
	 * @param bridge
	 *            the bridge
	 */
	public RemoteControl(final RoboBridge bridge) {
		super("RemoteControl");
		this.bridge = bridge;
		base = (RP6Base) bridge.getRobot().getDevice(1);
		setLayout(new GridLayout(3, 3));

		final JButton st = new JButton("ENABLE");
		st.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				if (executing) {
					bridge.setExecutingProgram(true);
					executing = false;
					st.setText("ENABLE");
				} else {
					bridge.setExecutingProgram(false);
					executing = true;
					st.setText("DISABLE");
				}
			}
		});
		add(st);

		final AbstractAction fa = new AbstractAction("FWD") {

			@Override
			public void actionPerformed(final ActionEvent e) {
				if (executing && base.isMovementComplete()) {
					base.moveDistance(70, Direction.FWD, 10);
				}
			}
		};

		final AbstractAction la = new AbstractAction("LEFT") {

			@Override
			public void actionPerformed(final ActionEvent e) {
				if (executing && base.isMovementComplete()) {
					base.rotate(70, Direction.LEFT, 90);
				}
			}
		};

		final AbstractAction sa = new AbstractAction("STOP") {

			@Override
			public void actionPerformed(final ActionEvent e) {
				if (executing) {
					base.stop();
				}
			}
		};

		final AbstractAction ra = new AbstractAction("RIGHT") {

			@Override
			public void actionPerformed(final ActionEvent e) {
				if (executing && base.isMovementComplete()) {
					base.rotate(70, Direction.RIGHT, 90);
				}
			}
		};

		final AbstractAction ba = new AbstractAction("BWD") {

			@Override
			public void actionPerformed(final ActionEvent e) {
				if (executing && base.isMovementComplete()) {
					base.moveDistance(70, Direction.BWD, 10);
				}
			}
		};

		final JButton fwd = new JButton(fa);
		add(fwd);

		add(new JPanel());

		final JButton left = new JButton(la);
		add(left);

		final JButton stop = new JButton(sa);
		add(stop);

		final JButton right = new JButton(ra);
		add(right);

		add(new JPanel());

		final JButton bwd = new JButton(ba);
		add(bwd);

		getActionMap().put("FWD", fa);
		getActionMap().put("RIGHT", ra);
		getActionMap().put("STOP", sa);
		getActionMap().put("LEFT", la);
		getActionMap().put("BWD", ba);
		getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke('w'),
				"FWD");
		getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke('a'),
				"LEFT");
		getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke('s'),
				"BWD");
		getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke('d'),
				"RIGHT");
		getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke('x'),
				"STOP");
		setSize(300, 300);
		setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * tc.de.krawallmann.robobridge.devices.SensorListener#onSensorValueChanged
	 * (tc.de.krawallmann.robobridge.devices.AbstractDevice, java.lang.String,
	 * java.lang.Object, java.lang.Object)
	 */
	@Override
	public void onSensorValueChanged(final AbstractDevice sensor, final String propertyName,
			final Object oldValue, final Object newValue) {
		if (executing && propertyName.equals(RP6Base.BUMPERS_LEFT)
				|| propertyName.equals(RP6Base.BUMPERS_RIGHT)) {
			base.stop();
		}
	}

}
